﻿@page "/issue/update/{Id}"
@inject IIssueService IssueService
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
@inject IMainPlanService MainPlanService

<PageTitle>更新项目异常描述</PageTitle>
<div class="row">
    <div class="col">
        <h3>
            @_mainPlanDto.Number-@_mainPlanDto.Name
            <a href="issues/mainplan/@_mainPlanDto.Id">[查看项目异常]</a>
        </h3>
    </div>
    <CompDeleteIcon Roles="admin,mgr,pm" Click="Delete" />
</div>
<EditForm Model="_issueDto" OnSubmit="Save">
    <div class="input-group mb-2">
        <span class="input-group-text">异常类型</span>
        <select class="form-select" @bind="@_issueDto.Title">
            <CompEnumList Type="typeof(IssueTitle_e)" />
        </select>
    </div>
    <div class="input-group mb-2">
        <span class="input-group-text">异常内容</span>
        <textarea class="form-control" placeholder="请详细描述异常，可输入多行" style="height: 180px" @bind="@_issueDto.Content"></textarea>
    </div>

    <ImagesUpload Title="异常图片(可添加多个)" @bind-MultiImageUrl="@_issueDto.ContentUrl" />

    <CompSaveCancelButtons CancelUrl="@($"issues/mainplan/{_issueDto.MainPlanId}")"/>
</EditForm>
@code {
    [Parameter]
    public string Id { get; set; }
    private IssueDto _issueDto = new();
    private MainPlanDto _mainPlanDto = new();

    protected override async Task OnParametersSetAsync()
    {
        var iResult = await IssueService.GetFirstOrDefaultAsync(Guid.Parse(Id));
        _issueDto = iResult.Result;
        
        var mResult = await MainPlanService.GetFirstOrDefaultAsync(_issueDto.MainPlanId);
        _mainPlanDto = mResult.Result;
    }
    private async Task Delete()
    {
        var confirmResult = await JsRuntime.InvokeAsync<bool>("confirm", "确定要删除当前异常吗？");
        if (confirmResult)
        {
            var result = await IssueService.DeleteAsync(Guid.Parse(Id));
            if (result.IsSuccessStatusCode) NavigationManager.NavigateTo($"issues/mainplan/{_issueDto.MainPlanId}");
        }
    }
    private async Task Save()
    {
        var result = await IssueService.UpdateAsync(Guid.Parse(Id), _issueDto);
        if (result.IsSuccessStatusCode) NavigationManager.NavigateTo($"issues/mainplan/{_issueDto.MainPlanId}");
    }
}
